<?php
    use yii\helpers\Html;
    use yii\helpers\Url;
    use yii\widgets\ActiveForm;
?>
<div class="wrapper wrapper-content">
    <div class="row">
        <div class="col-sm-12">

            <div class="ibox ">
                <div class="ibox-title">
                    <h5><?=Html::a('<i class="fa fa-angle-left"></i> 返回','javascript:history.go(-1);',['class'=>'btn btn-xs btn-white'])?> 编辑权限</h5>
                </div>
                <div class="ibox-content">
                    <?= Html::beginForm('','post',['id' => 'login-form','class' => 'form-horizontal m-t pass-form'])?>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><span class="text-danger">*&nbsp;</span>权限名称：</label>
                            <div class="col-sm-2">
                                <?=Html::activeInput('text',$model, 'name',['class'=>'form-control']) ?>
                                <?=Html::error($model,'name',['class'=>'input_err'])?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>权限级别：</label>
                            <div class="col-sm-3">
                                <?=Html::radioList('PermissionModel[level]',$model->level,['1'=>'顶级菜单','2'=>'子集菜单','3'=>'操作']);?>
                                <?=Html::error($model,'level',['class'=>'input_err'])?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><span class="text-danger">*</span>权限路由：</label>
                            <div class="col-sm-2">
                                <?=Html::activeInput('text',$model, 'url',['class'=>'form-control']) ?>
                                <?=Html::error($model,'url',['class'=>'input_err'])?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label"><span class="text-danger">*&nbsp;</span>所属菜单：</label>
                            <?php if($class_list) foreach($class_list as $key => $item){ ?>
                                <div class="col-sm-2">
                                    <?=$item?>
                                    <?php if($key == 0) echo Html::error($model,'parent_id',['class'=>'input_err']);?>
                                </div>
                            <?php }?>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">菜单权限码：</label>
                            <div class="col-sm-2">
                                <?=Html::activeInput('text',$model, 'menu_code',['class'=>'form-control','placeholder'=>'示例：101010']) ?>
                                <?=Html::error($model,'menu_code',['class'=>'input_err'])?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">按钮权限码：</label>
                            <div class="col-sm-2">
                                <?=Html::activeInput('text',$model, 'btn_code',['class'=>'form-control','placeholder'=>'示例：101010']) ?>
                                <?=Html::error($model,'btn_code',['class'=>'input_err'])?>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-4 col-sm-offset-2">
                                <?=Html::activeInput('hidden',$model, 'id') ?>
                                <?=Html::submitButton('保存',['class'=>'btn btn-primary col-md-offset-2'])?>
                                <?=Html::a('返回','javascript:history.go(-1);',['class'=>'btn btn-white'])?>
                            </div>
                        </div>
                    <?=Html::endForm() ?>
                </div>
            </div>
        </div>
    </div>
    <script>
        //分类联动
        $('body').on('change','.menu_class', function () {
            var _this = $(this);
            var menu_id = $(this).val();
            _this.parent('div').nextAll('div').remove();
            if(!menu_id || menu_id==0){
                return false;
            }
            $.ajax({
                url: '/admin-role-permission/menu-list',
                type: 'post',
                dataType: 'json',
                data: {id:menu_id},
                success: function (res) {
                    if(res.code == 20000){
                        var html = "<div class='col-sm-2'><select class='form-control menu_class' name='PermissionModel[parent_id]'><option value='' >请选择</option>";
                        $.each(res.data,function(i,n){
                            html += '<option value="' + i + '" >' + n + '</option>';
                        })
                        html += "</select></div>";
                        console.log(html);
                        _this.parent('div').after(html);
                    }
                }
            });
        });
    </script>